<template>
    <van-button
      round
      size="small"
      v-if="value"
      @click="isFollowed"
      :loading="isbtnLoading"
    >已关注</van-button>
    <van-button
      v-else
      type="info"
      color="#3296fa"
      round
      size="small"
      icon="plus"
      @click="isFollowed"
      :loading="isbtnLoading"
    >关注</van-button>
</template>

<script>
import { attentionUser, cancelAttention } from '@/api/user'
export default {
  name: 'attentionUser',
  components: {},
  props: {
    value: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data() {
    return {
      isbtnLoading: false // 关注用户加载状态
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 是否关注用户
    async isFollowed() {
      this.isbtnLoading = true
      let message = '操作失败，请重试'
      try {
        if (this.value) {
          // 已关注
          await cancelAttention(this.autId)
        } else {
          // 未关注
          await attentionUser(this.autId)
        }
        // this.article.is_followed = !this.article.is_followed
        this.$emit('input', !this.value)
      } catch (err) {
        if (err.response && err.response.status === 400) {
          message = '用户不能自己关注自己'
        }
        this.$toast.fail(message)
      }
      this.isbtnLoading = false
    }
  }
}
</script>

<style scoped lang="less">
</style>
